package com.tanhua.sso.config;

/**
 * @author 阿离(灰原)
 * @version 1.0
 * @date 2023/8/27 下午 11:27
 */

import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
import org.springframework.web.filter.CorsFilter;

/**
 * 允许跨域访问
 */
@Configuration
public class CorsConfig {
    public CorsFilter corsFilter() {
        CorsConfiguration corsConfiguration = new CorsConfiguration();
        corsConfiguration.addAllowedOrigin("*"); // 允许所有的请求来源
        corsConfiguration.addAllowedMethod("*"); // 允许所有的HTTP方法
        corsConfiguration.setAllowCredentials(true); // 允许携带凭证信息（如Cookie等）

        UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
        source.registerCorsConfiguration("/**", corsConfiguration);

        return new CorsFilter(source);
    }
}
